package com.example.skyline.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.skyline.entity.Menu;
import com.example.skyline.entity.Role;
import com.example.skyline.entity.UserPermission;
import com.example.skyline.mapper.MenuMapper;
import com.example.skyline.mapper.RoleMapper;
import com.example.skyline.mapper.UserPerMissionMapper;
import com.example.skyline.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserPerMissionMapper userPerMissionMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Role> fetchMenusForRoleAll() {
        LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<Role>();
        List<Role> list = roleMapper.selectList(roleLambdaQueryWrapper);// 获取所有角色 不加任何条件

        for (Role role : list) {
            LambdaQueryWrapper<UserPermission> userPermissionLambdaQueryWrapper = new LambdaQueryWrapper<UserPermission>();
            userPermissionLambdaQueryWrapper.eq(UserPermission::getRoleId, role.getRoleId());
            List<UserPermission> userPermissionList = userPerMissionMapper.selectList(userPermissionLambdaQueryWrapper);
            for (UserPermission userPermission : userPermissionList) {
                LambdaQueryWrapper<Menu> menuLambdaQueryWrapper = new LambdaQueryWrapper<Menu>();
                menuLambdaQueryWrapper.eq(Menu::getMenuId, userPermission.getMenuId());
                Menu menu = menuMapper.selectOne(menuLambdaQueryWrapper);
                userPermission.setMenuName(menu.getMenuName());
            }

            role.setUserPermissions(userPermissionList);
        }

        return list;
    }

    @Override
    public void editRoleMenu(Integer roleId, List<Integer> menuRole) {
        LambdaQueryWrapper<UserPermission> userPermissionLambdaQueryWrapper = new LambdaQueryWrapper<UserPermission>();
        userPermissionLambdaQueryWrapper.eq(UserPermission::getRoleId, roleId);
        // 删除现有的权限
        userPerMissionMapper.delete(userPermissionLambdaQueryWrapper); // 删除RoleId 所有的权限
        // 新增新的权限
        for (Integer menuId : menuRole) {
            UserPermission userPermission = new UserPermission();
            userPermission.setRoleId(roleId);
            userPermission.setMenuId(menuId);
            userPerMissionMapper.insert(userPermission);
        }
    }

    @Override
    public Integer addRole(Role role) {
        if(role.getRoleName() == null || role.getRoleName().equals("")) {
            return 0;
        }

        // 判断角色名是否已存在
        LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<Role>();
        roleLambdaQueryWrapper.eq(Role::getRoleName, role.getRoleName());
        Role dbRole = roleMapper.selectOne(roleLambdaQueryWrapper);
        if(dbRole != null) {
            return -1;
        }
        return roleMapper.insert(role);
    }

    @Override
    public List<Role> fetchRoleAll() {
        return roleMapper.selectList(null);
    }

    // -----------------------------------------------------------------------------------------------------------------

    @Override
    public int insertRole(Role role) {
        return roleMapper.insert(role);
    }

    @Override
    public int deleteRole(Integer id) {
        return roleMapper.deleteById(id);
    }

    @Override
    public int updateRole(Role role) {
        return  roleMapper.updateById(role);
    }

    @Override
    public Role getRoleById(Integer id) {
        return roleMapper.selectById(id);
    }

    @Override
    public List<Role> getRoleListByRole(Integer start, Integer pageSize) {
        return roleMapper.getRoleListByRole(start, pageSize);
    }


    @Override
    public Integer getRoleCount() {
        return roleMapper.getRoleCount();
    }

    @Override
    public List<Role> selectRoleList(){
        return roleMapper.selectRoleList();
    }






}
